﻿/*

filipesilvestrim - An open-source ActionScript 3.0 repository of codes for Game Developers 

http://code.google.com/p/filipesilvestrim/

Copyright (c) 2009 Filipe Ghesla Silvestrim, All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

package com.filipesilvestrim.game.level
{
	
	import com.filipesilvestrim.error.AbstractError;
	import com.filipesilvestrim.game.core.IGameplay;
	import com.filipesilvestrim.game.core.IProcessable;
	import com.filipesilvestrim.manager.IBuilder;
	import com.filipesilvestrim.status.state.BitState;
	import com.filipesilvestrim.status.state.IState;
	import com.filipesilvestrim.util.time.Stopwatch;
	
	import flash.display.Sprite;
	import flash.utils.Dictionary;

	public class AbstractLevel extends Sprite implements IState, IProcessable
	{
		// ___________________________________________________________________ CONSTANTS
		static public const RUNNING		: String	= "level_state_running";
		static public const IDLE		: String	= "level_state_idle";
		static public const PAUSED		: String	= "level_state_paused";
		static public const STOPPED		: String	= "level_state_stopped";
		static public const END			: String	= "level_state_end";
		
		// ___________________________________________________________________ CLASS PROPERTIES
		public var wasBuild			: Boolean;
		public var endTime			: Number;
		
		protected var _id			: int;
		protected var _difficulty	: int;
		protected var _won			: Boolean;
		protected var _isBlocked	: Boolean;
		
		private var _totalTime		: Number;
		private var _halfTime		: Number;
		
		// ___________________________________________________________________ INSTANCE PROPERTIES
		protected var _scenes			: *;//space
		protected var _state			: BitState;
		protected var _gameplay			: IGameplay;
		protected var _dicTriggers		: Dictionary	= new Dictionary(true);		
		protected var _dicGoals			: Dictionary	= new Dictionary(true);		
		protected var _dicObjects		: Dictionary	= new Dictionary(true);		
		protected var _dicCharacters	: Dictionary	= new Dictionary(true);		
		protected var _dicBarriers		: Dictionary	= new Dictionary(true);		
		protected var _clock			: Stopwatch		= new Stopwatch();
		protected var _info				: Object		= new Object();
		protected var _props			: Object		= new Object();
		public var callbackClock		: Function 		= null;
		
		// ___________________________________________________________________ GETTERS AND SETTERS
		public function get id () 		: * { return _id; }
		
		// ___________________________________________________________________ CONSTRUCTOR
		/**
		 * time in seconds
		 */
		public function AbstractLevel ( id : int, levelTime : Number = -1 ) 
		{
			_id						= id;
			
			_clock					= new Stopwatch();
			_clock.callbackTick 	= onTick;
			_state					= new BitState([RUNNING, IDLE, PAUSED, STOPPED, END]);
			
			if (levelTime != -1)
				addTime(levelTime);
		}
		
		// ___________________________________________________________________ PUBLIC METHODS
		public function build (...args) : void
		{
			throw new AbstractError(AbstractError.MUST_OVERRIDE);
		}
		
		public function destroy (...args) : void
		{
			throw new AbstractError(AbstractError.MUST_OVERRIDE);
		}
		
		public function enable () 	: void {}
		public function disable () 	: void {}
		
		public function start () 	: void 
		{
			endTime = 0;
			_state.unsetState(PAUSED);
			_state.unsetState(STOPPED);
			_state.setState(RUNNING);
			
			_clock.reset();
			_clock.addLap(_totalTime);
			_clock.addLap(_halfTime);
			_clock.start();
		}
		
		public function pause () 	: void 
		{
			_state.setState(PAUSED);
			_clock.stop();
		}
		
		public function stop () 	: void 
		{
			_state.unsetState(RUNNING);
			_state.setState(STOPPED);
			_clock.stop();
		}
		
		public function addTime ( time : int ) : void
		{
			_totalTime				= time * 1000;
			_halfTime				= _totalTime * .5;
		}
		
		public function getPropertie ( name : String ) : *
		{
			return _props[name];
		}
		
		// ___________________________________________________________________ PRIVATE METHODS
		internal function changePropertie ( name : String, value : *) : void
		{
			this[name] = value;
		}
		
		internal function addPropertie ( name : String, value : *) : void
		{
			_props[name] = value;
		}
		
		private function onTick (elapsed : Number) : void
		{
			endTime = elapsed;//_clock.getSecond(-1);
			
			if (callbackClock != null)
				callbackClock(_clock);
			
			if(_clock.checkLap(0))
				_gameplay.gameOver();
		}
		
		// ___________________________________________________________________ EVENTS
		
	}
}